from typing import List, Any
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session

from ....api import deps
from ....schemas.user import UserResponse, UserUpdate
from ....models.user import User
from ....crud.user import user_crud

router = APIRouter()

@router.get("/me", response_model=UserResponse)
def read_user_me(
        current_user: User = Depends(deps.get_current_user)
) -> Any:
    """
    获取当前登录用户信息
    """
    return current_user

@router.put("/me", response_model=UserResponse)
def update_user_me(
        user_in: UserUpdate,
        current_user: User = Depends(deps.get_current_user),
        db: Session = Depends(deps.get_db)
) -> Any:
    """
    更新当前登录用户信息
    """
    user = user_crud.update(db, db_obj=current_user, obj_in=user_in)
    return user

@router.get("/{user_id}", response_model=UserResponse)
def read_user_by_id(
        user_id: int,
        current_user: User = Depends(deps.get_current_user),
        db: Session = Depends(deps.get_db)
) -> Any:
    """
    通过ID获取用户信息
    """
    user = user_crud.get(db, id=user_id)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="用户不存在"
        )
    return user